﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAL
{
    public partial class UserService : GetData
    {
        CRMEntities db;

        public List<UserEntity> LoadAllUser()
        {
            db = new CRMEntities();
            var query = (from user in db.Users
                         select new UserEntity
                         {
                             UserID = user.User_ID,
                             FirstName = user.FirstName,
                             LastName = user.LastName,
                             TelephoneNumber = user.TelephoneNumber,
                             Account = user.Account,
                             Password = user.Password,
                             Region_Code = user.Region_Code,
                             Role = (int)user.Role,
                             Disable = user.IsDisable
                         }).ToList();

            return query;
        }

        public UserEntity LoadUserByID(int UserID)
        {
            db = new CRMEntities();
            var query = (from user in db.Users
                         where user.User_ID == UserID
                         select new UserEntity
                         {
                             UserID = user.User_ID,
                             FirstName = user.FirstName,
                             LastName = user.LastName,
                             TelephoneNumber = user.TelephoneNumber,
                             Account = user.Account,
                             Password = user.Password,
                             Region_Code = user.Region_Code,
                             Role = (int)user.Role,
                             Disable = user.IsDisable
                         }).SingleOrDefault();

            return query;
        }

        public UserEntity LoadUserByAccount(string Account)
        {
            db = new CRMEntities();
            var query = (from user in db.Users
                         where user.Account == Account
                         select new UserEntity
                         {
                             UserID = user.User_ID,
                             FirstName = user.FirstName,
                             LastName = user.LastName,
                             TelephoneNumber = user.TelephoneNumber,
                             Account = user.Account,
                             Password = user.Password,
                             Region_Code = user.Region_Code,
                             Role = (int)user.Role,
                             Disable = user.IsDisable
                         }).SingleOrDefault();

            return query;
        }

        public List<UserEntity> LoadUserbyRole(int role) 
        {
            db = new CRMEntities();
            var query = (from user in db.Users.Where(a => a.IsDisable == false && a.Role == role)
                         orderby user.Region_Code
                         select new UserEntity
                         {
                             UserID = user.User_ID,
                             FirstName = user .FirstName,
                             LastName = user .LastName ,
                             TelephoneNumber = user .TelephoneNumber ,
                             Account = user .Account ,
                             Role = (int)user.Role,
                             Region_Code = user.Region_Code,
                         });
            return query.ToList();
        }



    }
}
